home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / icl8 To cicn / Source / icl8 To cicn.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  5.1 KB  |  205 lines  |  [TEXT/CWIE]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    icl8 To cicn                                            */
  4. /*                                                                            */
  5. /*    Description:    This snippet converts a 'icl8' and 'ICN#' resource         */
  6. /*                    to a 'cicn' resource.                                    */
  7. /*                                                                            */
  8. /*    Files:            icl8 To cicn.π                                            */
  9. /*                    icl8 To cicn.c                                            */
  10. /*                    icl8 To cicn.π.rsrc                                        */
  11. /*                                                                            */
  12. /*    Programmer:        Edgar Lee                                                */
  13. /*    Organization:    Apple Computer, Inc.                                    */
  14. /*    Department:        Developer Technical Support, DTS                        */
  15. /*    Language:        C (Think C version 5.0.2)                                */
  16. /*    Date Created:    12-29-92                                                */
  17. /*                                                                            */
  18. /****************************************************************************/
  19.  
  20. #include <Dialogs.h>
  21. #include <Fonts.h>
  22. #include <Resources.h>
  23. #include <Icons.h>
  24.  
  25. /* Constant Declarations */
  26.  
  27. #define    WWIDTH        320
  28. #define    WHEIGHT        320
  29.  
  30. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  31. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  32.  
  33. /* Global Variable Definitions */
  34.  
  35. WindowPtr            gWindow;
  36. CIconHandle            gCICN;
  37.  
  38. void initMac();
  39. void createWindow();
  40. void doEventLoop();
  41.  
  42. void icl8ToCICN();
  43. void drawIcon();
  44.  
  45. void main(void)
  46. {
  47.     initMac();
  48.     
  49.     createWindow();
  50.     icl8ToCICN();
  51.  
  52.     doEventLoop();
  53. }
  54.  
  55. void initMac()
  56. {
  57.     MaxApplZone();
  58.     
  59.     InitGraf( &qd.thePort );
  60.     InitFonts();
  61.     InitWindows();
  62.     InitMenus();
  63.     TEInit();
  64.     InitDialogs( nil );
  65.     InitCursor();
  66.     FlushEvents( 0, everyEvent );
  67. }
  68.  
  69. void createWindow()
  70. {
  71.     Rect rect;
  72.     
  73.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  74.     
  75.     gWindow = NewCWindow( 0L, &rect, "\p'icl8' To 'cicn'", true, noGrowDocProc,
  76.                             (WindowPtr)-1L, true, 0L );
  77.     SetPort( gWindow );
  78. }
  79.  
  80. void icl8ToCICN()
  81. {
  82.     Handle        icn;            /* Handle to the icon's bitmap image and mask. */
  83.     Handle        icl8;            /* Handle to the icl8 resource. */
  84.     char        depth;            /* Depth of the icl8 pixel image. */
  85.     Rect        bounds;            /* Bounding rect for the icon. */
  86.     long        bitmapSize;        /* Size of the icon's bitmap. */
  87.  
  88.     SetRect( &bounds, 0, 0, 32, 32 );    /* 'icl8' are 32x32 pixels. */
  89.     depth = 8;                            /* 8-bit deep pixel image. */
  90.     bitmapSize = 4 * 32;                /* 4 bytesPerRow * 32 rows. */
  91.     
  92.     /* Load and lock the 'icl8' and 'ICN#' resources used to build the 'cicn'. */
  93.     
  94.     icn = GetResource( 'ICN#', 128 );
  95.     icl8 = GetResource( 'icl8', 128 );
  96.     
  97.     HLock( icn );
  98.     HNoPurge( icn );
  99.     
  100.     HLock( icl8 );
  101.     HNoPurge( icl8 );
  102.         
  103.     /* Allocate memory for the 'cicn'. */
  104.     
  105.     gCICN = (CIconHandle)NewHandleClear( (long)sizeof( CIcon ) );
  106.     
  107.     /* Fill in the cicn's bitmap fields. */ 
  108.     
  109.     (**gCICN).iconBMap.baseAddr                = nil;
  110.     (**gCICN).iconBMap.rowBytes                = 4;
  111.     (**gCICN).iconBMap.bounds                = bounds;
  112.  
  113.     /* Fill in the cicn's mask bitmap fields. */
  114.     
  115.     (**gCICN).iconMask.baseAddr                = nil;
  116.     (**gCICN).iconMask.rowBytes                = 4;
  117.     (**gCICN).iconMask.bounds                = bounds;
  118.     
  119.     /* Fill in the cicn's pixmap fields. */
  120.     
  121.     (**gCICN).iconPMap.baseAddr                = nil;
  122.     (**gCICN).iconPMap.rowBytes                = (((bounds.right - bounds.left) * depth) / 8) | 0x8000;
  123.     (**gCICN).iconPMap.bounds                = bounds;
  124.     (**gCICN).iconPMap.pmVersion            = 0;
  125.     (**gCICN).iconPMap.packType                = 0;
  126.     (**gCICN).iconPMap.packSize                = 0;
  127.     (**gCICN).iconPMap.hRes                    = 72;
  128.     (**gCICN).iconPMap.vRes                    = 72;
  129.     (**gCICN).iconPMap.pixelSize            = depth;
  130.     (**gCICN).iconPMap.planeBytes            = 0;
  131.     (**gCICN).iconPMap.pmReserved            = 0;
  132.     (**gCICN).iconPMap.pixelType            = 0;
  133.     (**gCICN).iconPMap.cmpCount                = 1;
  134.     (**gCICN).iconPMap.cmpSize                = depth;
  135.     (**gCICN).iconPMap.pmTable                = GetCTable( depth );
  136.  
  137.     /* Set the 'icl8' pixel image to the iconData field. */
  138.     
  139.     (**gCICN).iconData = (Handle)icl8;
  140.     
  141.     /* Resize the 'cicn' for the bitmap image and mask. */
  142.     
  143.     SetHandleSize( (Handle)gCICN, sizeof( CIcon ) + (bitmapSize * 2) );
  144.     
  145.     /* Copy the 'ICN#' data into the iconMaskData array. */
  146.     /* Note1: This is an array of shorts, so divide bitmapSize by 2. */
  147.     /* Note2: The mask comes before the image.  The is opposite of an 'ICN#' */
  148.  
  149.     BlockMove( *icn, &(**gCICN).iconMaskData[bitmapSize / 2], bitmapSize );        /* The 1bit image. */
  150.     BlockMove( *icn + (long)bitmapSize, (**gCICN).iconMaskData, bitmapSize );    /* The mask. */
  151. }
  152.  
  153. void drawIcon()
  154. {
  155.     /* Paint the background red to see that the icon's mask does work. */
  156.     
  157.     ForeColor( redColor );
  158.     PaintRect( &(*gWindow).portRect );
  159.     
  160.     /* Now, draw the 'cicn' */
  161.     
  162.     PlotCIcon( &(*gWindow).portRect, gCICN );
  163. }
  164.  
  165. void doEventLoop()
  166. {
  167.     EventRecord        event;
  168.     WindowPtr        window;
  169.     short            clickArea;
  170.     Rect            screenRect;
  171.  
  172.     for (;;)
  173.     {
  174.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  175.         {
  176.             if (event.what == mouseDown)
  177.             {
  178.                 clickArea = FindWindow( event.where, &window );
  179.                 
  180.                 if (clickArea == inDrag)
  181.                 {
  182.                     screenRect = (**GetGrayRgn()).rgnBBox;
  183.                     DragWindow( window, event.where, &screenRect );
  184.                 }
  185.                 else if (clickArea == inContent)
  186.                 {
  187.                     if (window != FrontWindow())
  188.                         SelectWindow( window );
  189.                 }
  190.                 else if (clickArea == inGoAway)
  191.                     if (TrackGoAway( window, event.where ))
  192.                         return;
  193.             }
  194.             else if (event.what == updateEvt)
  195.             {
  196.                 window = (WindowPtr)event.message;    
  197.                 SetPort( window );
  198.                 
  199.                 BeginUpdate( window );
  200.                 drawIcon();
  201.                 EndUpdate( window );
  202.             }
  203.         }
  204.     }
  205. }